// 统一的日志管理工具（浏览器彩色 + 分组版）
export enum LogLevel {
  DEBUG = 0,
  INFO = 1,
  WARN = 2,
  ERROR = 3
}

export interface LoggerConfig {
  level: LogLevel;
  showTimestamp: boolean;
  showLevel: boolean;
  useColor: boolean;
}

export class Logger {
  private config: LoggerConfig = {
    level: LogLevel.INFO,
    showTimestamp: true,
    showLevel: true,
    useColor: true
  };
  private configShown = false;

  private styles: Record<LogLevel, string> = {
    [LogLevel.DEBUG]: 'color:#595959;background:#f5f5f5;padding:2px 4px;border-radius:4px;',
    [LogLevel.INFO]: 'color:#096dd9;background:#e6f7ff;padding:2px 4px;border-radius:4px;',
    [LogLevel.WARN]: 'color:#d46b08;background:#fff7e6;padding:2px 4px;border-radius:4px;',
    [LogLevel.ERROR]: 'color:#a8071a;background:#fff1f0;padding:2px 4px;border-radius:4px;'
  };

  setLevel(level: LogLevel) {
    this.config.level = level;
  }

  setDisplayOptions(options: Partial<LoggerConfig>) {
    Object.assign(this.config, options);
  }

  showCurrentConfig() {
    const configInfo = [
      `级别: ${LogLevel[this.config.level]}`,
      `时间戳: ${this.config.showTimestamp ? '✓' : '✗'}`,
      `级别标签: ${this.config.showLevel ? '✓' : '✗'}`,
      `彩色: ${this.config.useColor ? '✓' : '✗'}`
    ].join(' | ');

    console.log(`🔧 日志配置: ${configInfo}`);
  }

  resetConfigDisplay() {
    this.configShown = false;
  }

  private formatPrefix(level: LogLevel): string {
    const parts: string[] = [];
    if (this.config.showTimestamp) parts.push(new Date().toLocaleTimeString());
    if (this.config.showLevel) parts.push(`[${LogLevel[level]}]`);
    return parts.join(' ');
  }

  private shouldLog(level: LogLevel) {
    return level >= this.config.level;
  }

  private log(level: LogLevel, message: string, ...args: any[]) {
    if (!this.shouldLog(level)) return;
    if (!this.configShown) {
      this.showCurrentConfig();
      this.configShown = true;
      console.log('');
    }
    const prefix = this.formatPrefix(level);
    const style = this.config.useColor ? this.styles[level] : '';
    const fullMessage = prefix ? `${prefix} ${message}` : message;
    switch (level) {
      case LogLevel.DEBUG:
        console.debug(`%c${fullMessage}`, style, ...args);
        break;
      case LogLevel.INFO:
        console.info(`%c${fullMessage}`, style, ...args);
        break;
      case LogLevel.WARN:
        console.warn(`%c${fullMessage}`, style, ...args);
        break;
      case LogLevel.ERROR:
        console.error(`%c${fullMessage}`, style, ...args);
        break;
    }
  }

  debug(message: string, ...args: any[]) {
    this.log(LogLevel.DEBUG, message, ...args);
  }
  info(message: string, ...args: any[]) {
    this.log(LogLevel.INFO, message, ...args);
  }
  warn(message: string, ...args: any[]) {
    this.log(LogLevel.WARN, message, ...args);
  }
  error(message: string, ...args: any[]) {
    this.log(LogLevel.ERROR, message, ...args);
  }

  group(label: string, level: LogLevel = LogLevel.INFO) {
    if (!this.shouldLog(level)) return;
    const prefix = this.formatPrefix(level);
    const style = this.config.useColor ? this.styles[level] : '';
    console.group(`%c${prefix} ${label}`, style);
  }

  groupEnd() {
    console.groupEnd();
  }
}

// 导出全局实例
export const logger = new Logger();

// 这里导出 Logger 类本身类型
export type LoggerType = Logger;
